﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeadVinaglue.Models;
using LeadVinaglue.Models.ViewPages;

namespace LeadVinaglue.Repository
{
    public class ArchiveTypeManagementRepository : BaseManagementRepository
    {
        LeadVinaglueEntities entities = new LeadVinaglueEntities();

        public ArchiveTypeManagementModel GetModel(string pageType, string roleName, int id)
        {
            ArchiveTypeManagementModel model = new ArchiveTypeManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.ListArchiveType = GetAll();

            if (id > 0)
            {
                model.ArchiveTypeInfo = GetSingle(id);
            }

            return model;
        }

        public ArchiveTypeManagementModel GetModel(string pageType, string roleName)
        {
            ArchiveTypeManagementModel model = new ArchiveTypeManagementModel();
            model.ListArchiveType = GetAll();

            return model;
        }

        public List<ArchiveTypeModel> GetAll()
        {
            List<ArchiveTypeModel> list = new List<ArchiveTypeModel>();

            var items = (from c in entities.ArchiveTypes
                         orderby c.DateCreated descending
                         select c).ToList();

            foreach (var item in items)
            {
                ArchiveTypeModel model = new ArchiveTypeModel();

                model.ID = item.ID;
                model.ParentId = (item.ParentID != null) ? item.ParentID.Value : 0;
                model.Ordered = (item.SortOrder != null) ? item.SortOrder.Value : 0;
                model.Name = item.Name;
                model.ModuleName = item.ModuleName;
                model.DateCreated = Convert.ToDateTime(item.DateCreated).ToString("dd-MM-yyyy hh:mm tt");
                model.Active = item.Active;
                list.Add(model);
            }

            return list;
        }

        public ArchiveType GetSingle(int id)
        {
            return (from _object in entities.ArchiveTypes where _object.ID == id select _object).FirstOrDefault();
        }

        public ArchiveType GetSingle(string name)
        {
            return (from _object in entities.ArchiveTypes where _object.Name == name select _object).FirstOrDefault();
        }

        public void Create([Bind(Exclude = "ID, DateCreated")] ArchiveType _object)
        {
            _object.DateCreated = System.DateTime.Now;
            entities.AddToArchiveTypes(_object);
            entities.SaveChanges();
        }

        public void Delete(int id)
        {
            var _object = (from _list in entities.ArchiveTypes where _list.ID == id select _list).First();
            entities.DeleteObject(_object);
            entities.SaveChanges();
        }

        public void Save([Bind(Exclude = "ID")] ArchiveType _object)
        {
            _object.EntityKey = (from ArchiveType ac in entities.ArchiveTypes where ac.ID == _object.ID select ac).FirstOrDefault().EntityKey;
            _object.DateCreated = System.DateTime.Now;
            entities.ApplyPropertyChanges(_object.EntityKey.EntitySetName, _object);
            entities.SaveChanges();
        }
    }
}